package com.starry.home.ui.main


import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.starry.core.base.BaseActivity
import com.starry.core.ext.singleClick
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivityMainBinding
import com.starry.home.ui.adapter.ViewPager2Adapter
import com.starry.home.ui.frament.HomeFragment
import com.starry.home.ui.frament.MineFragment
import com.starry.home.ui.mine.UserInfoActivity
import com.starry.home.viewmodel.main.MainViewModel
import com.yarolegovich.slidingrootnav.SlideGravity
import com.yarolegovich.slidingrootnav.SlidingRootNav
import com.yarolegovich.slidingrootnav.SlidingRootNavBuilder
import com.yarolegovich.slidingrootnav.callback.DragStateListener


/**
 * 项目主界面
 */
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {


    private lateinit var mSlidingRootNav: SlidingRootNav
    private lateinit var mLLMenu:LinearLayout

    private val fragments: MutableList<Fragment> = ArrayList()
    private val titles: MutableList<String> = ArrayList();

    companion object {
        fun startAction(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }


    override val shouldAddStatusHeight: Boolean
        get() = true
    override fun initData() {
        mDataBinding.click = MainClick()

        val toolbar: Toolbar = getToolbar()
        setSupportActionBar(toolbar)

        /*mSlidingRootNav = SlidingRootNavBuilder(this)
            .withToolbarMenuToggle(toolbar)
            .withMenuOpened(false)
            .withContentClickableWhenMenuOpened(false)
            .withMenuLayout(R.layout.menu_left_drawer)
            .inject()*/
        toolbar.singleClick {
            mSlidingRootNav.openMenu()
        }
        mSlidingRootNav = SlidingRootNavBuilder(this)
            .withGravity(SlideGravity.LEFT)
            .withMenuOpened(false)
            .withContentClickableWhenMenuOpened(false)
            .withMenuLayout(R.layout.menu_left_drawer)
            .inject()

        mLLMenu = mSlidingRootNav.layout.findViewById<LinearLayout>(R.id.ll_menu)
        mLLMenu.visibility = View.GONE
        mSlidingRootNav.layout.addDragStateListener(object : DragStateListener {
            override fun onDragStart() {
                mLLMenu.visibility = View.VISIBLE
            }

            override fun onDragEnd(isMenuOpened: Boolean) {
                mLLMenu.visibility = View.VISIBLE
            }

        })
    }

    override fun initView() {
        fragments.addAll(
            listOf(
                HomeFragment.newInstance("home"),
                MineFragment.newInstance("mine")
            )
        )
        titles.addAll(
            listOf("就诊", "个人中心")
        )

        val pager2Adapter = ViewPager2Adapter(this@MainActivity, fragments)
        mDataBinding.vpMain.adapter = pager2Adapter

        val checkboxList = arrayListOf(mDataBinding.cbHome, mDataBinding.cbMine)
        mDataBinding.vpMain.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                checkboxList.forEachIndexed { index, checkBox ->
                    checkBox.isChecked = index == position
                }
            }
        })

    }


    inner class MainClick {
        //切换到主页
        fun home(view: View?) {
            mDataBinding.vpMain.setCurrentItem(0, false)
        }

        //切换个人中心
        fun mine(view: View?) {
            mDataBinding.vpMain.setCurrentItem(1, false)

        }

        //完善个人信息
        fun userInfo(view: View?) {
            UserInfoActivity.startAction(getContext())
        }
    }

    override fun getLayoutId() = R.layout.activity_main

}